﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Windows;
using Caliburn.Micro;
using OrbitOne.CRMLyncInfoDashboard.Loader.Views;
using OrbitOne.CRMLyncInfoDashboard.Shell;

namespace OrbitOne.CRMLyncInfoDashboard.Loader.ViewModels
{
    [Export(typeof(LoaderViewModel))]
    public class LoaderViewModel : Screen
    {
        public ActionExecutionContext  ActionExecutionContext { get; private set; }
        
        protected override void OnViewLoaded(object view)
        {
            base.OnViewLoaded(view);
            ActionExecutionContext = new ActionExecutionContext
                                         {
                                             View = ((LoaderView)view).busyIndicator,
                                             EventArgs = this
                                         };
            Coroutine.BeginExecute(GoForward().GetEnumerator(), ActionExecutionContext);
        }
       
        public IEnumerable<IResult> GoForward()
        {
            yield return Loader.Show("Downloading...");
            yield return new LoadCatalog("OrbitOne.CRMLyncInfoDashboard.Shell.xap");
            yield return Loader.Hide();
            yield return new ShowScreen();
        }
    }
}